#!/usr/bin/env python
#-*- coding:utf-8 -*-

from xml.etree.ElementTree import ElementTree
from wordpath import *

class Parse():
    def __init__(self, grade):
        self.grade = grade
        self.tree = ElementTree()
        self.tree.parse(grade)

    def copyleft(self):
        '''返回非版权信息
        包括：书名：单词数：作者：最后修改日期
        '''
        copyleft = []
        # 书本头部处理
        head = self.tree.getiterator("head")
        #print head, len(head), type(head)
        head_child = head[0].getchildren()
        #print head_child, len(head_child), type(head_child)

        for item in head_child:
            copyleft.append(item.text)

        #print copyleft
        return copyleft

    def mean_summary(self):
        '''仅返回单词的意思，用于简单的列表显示
        '''
        word = self.tree.getiterator("word")
        word_list = []
        #print word, len(word), type(word)
        for num in range(100):
            word_content = word[num].getchildren()    # word[num]表示第num个单词的内容
            word_summary = {}

            #word_summary['index'] = word_content[0].text
            word_summary['name'] = word_content[1].text
            #print word_summary['name']
            # 单词有音标
            if word[num].find('pronounce') != None:
                #word_summary['pronounce'] = word_content[2].text
                word_summary['meaning'] = word_content[3].text
            else:
                #word_summary['pronounce'] = '' # 没有音标
                #print '%s Has no pronounce!' % word_summary['name']
                word_summary['meaning'] = word_content[2].text

            word_list.append(word_summary)

        return word_list

    def mean_detail(self, word_select):
        '''返回单词的详细解释
        参数：当前选中的单词
        '''
        word = self.tree.getiterator("word")
        #print word, len(word), type(word)
        for num in range(100):
            word_content = word[num].getchildren()    # word[num]表示第num个单词的内容
            word_detail = {}

            #word_detail['index'] = word_content[0].text
            word_detail['name'] = word_content[1].text

            if word_detail['name'] != word_select:
                continue
            else:
                # 单词有音标否？
                if word[num].find('pronounce') != None:
                    word_detail['pronounce'] = word_content[2].text
                    word_detail['meaning'] = word_content[3].text
                else:
                    word_detail['pronounce'] = '' # 没有音标
                    print '%s Has no pronounce!' % word_detail['name']
                    word_detail['meaning'] = word_content[2].text

                # 为音标加上括号
                word_detail['pronounce'] = '[' + word_detail['pronounce'] + ']'

                # 是一个列表，列表里面的元素为元组
                word_detail['sentence'] = []
                sen_all = word[num].findall('sentence')
                #print (sen_all[0].getchildren()[1].text)
                if sen_all is not None:
                    for sen_all_child in sen_all:
                        en = sen_all_child.getchildren()[0].text
                        zh = sen_all_child.getchildren()[1].text
                        word_detail['sentence'].append((en, zh))

                print word_detail['sentence']
                # 打印出最后的字典类
                #print word_detail
                return word_detail
        return None                     # 单词未找到

if __name__ == '__main__':
    test = Parse(BOOK_PATH + 'beginner/grade_2.xml')
#     print test.copyleft()
#     print test.mean_summary()
    print test.mean_detail('tiny')
